package com.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.AllData;
import com.entity.User;
import com.util.DataSourceUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.util.DataSourceUtils;
import com.entity.News;
@WebServlet("/newsupdate")
public class NewsupdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User manager=(User) session.getAttribute("user");
        if(manager==null)
        {
            req.getRequestDispatcher("/WEB-INF/jsp/login.jsp")
                    .forward(req, resp);
        }
        else{
            News items2 = null;
            String sql2 = "SELECT * FROM News WHERE id=?";
            try(Connection conn2 = DataSourceUtils.getConnection();
                PreparedStatement st2 = conn2.prepareStatement(sql2)){
                st2.setString(1,req.getParameter("uid"));
                try(ResultSet rs2 = st2.executeQuery()){
                    while(rs2.next()){
                        items2 = new News(rs2.getInt("id"), rs2.getString("name"), rs2.getString("content"), rs2.getTimestamp("date"));
                    }
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            req.setAttribute("items2", items2);
            req.setAttribute("op", req.getParameter("op"));
            req.getRequestDispatcher("/WEB-INF/jsp/newsupdate.jsp")
                    .forward(req, resp);
        }
    }
}